
import { _decorator, Component, Node, MeshRenderer, Color, color, v3, Tween, Material, Vec3, tween } from 'cc';
import yyComponent from '../../Script/Common/yyComponent';

const { ccclass, property } = _decorator;

@ccclass('AirplaneDriverLight')
export class AirplaneDriverLight extends yyComponent {
    @property(MeshRenderer)
    protected meshrender: MeshRenderer = null;
    protected mat: Material = null;

    protected colorCfg = {
        normal: { c: color().fromHEX('#C53C10'), floatMaxG: 80 },
        speedUp: { c: color().fromHEX('#1034C5'), floatMaxG: 100 }
    }
    protected _isSpeedUp = false;
    protected _isScaleAnim = false;
    protected _curScale: Vec3 = null;
    protected _curColor: Color = null;
    protected _floatColor = 0;
    protected _floatDirec = 1;
    protected _floatMaxG = 0;
    onLoad() {
        this.onEvents();
        this.onPlayerCtrl({ isSpeedUpBtn: true });
    }

    onEvents() {
        // this.on(CurrGameEvents.LevelScene.playerCtrlEvent, this.onPlayerCtrl, this);
    }

    onEnable() {
        this.init();
    }

    init() {
        this._isSpeedUp = false;
        this._isScaleAnim = false;
        Tween.stopAllByTarget(this._curScale);
        this._curScale = v3(1, 1, 1);
        this.node.setScale(this._curScale);
        Tween.stopAllByTarget(this._curColor);
        this._curColor = color(this.colorCfg.normal.c);
        this._floatColor = 0;
        this._floatMaxG = this.colorCfg.normal.floatMaxG;
        this._floatDirec = 1;
    }

    update(dt) {
        this.lightFloatAnim(dt);
        this.updateScale(dt);
    }
    //
    updateScale(dt) {
        if (!this._isScaleAnim) return;
        this.node.setScale(this._curScale);
    }

    protected _tmpColor = color();
    //火焰闪烁动画
    lightFloatAnim(dt) {
        this._floatColor += this._floatDirec * dt * this._floatMaxG * 20;
        if (this._floatColor >= this._floatMaxG) {
            this._floatColor = this._floatMaxG;
            this._floatDirec = -1;
        }
        if (this._floatColor <= 0) {
            this._floatColor = 0;
            this._floatDirec = 1;
        }
        if (!this.mat) {
            this.mat = this.meshrender.materials[0];
        }
        this._tmpColor.set(this._curColor);
        this._tmpColor.g = this._curColor.g + this._floatColor;
        this.mat.setProperty('mainColor', this._tmpColor);
    }

    /**
     *加速时变长/减速时变短 动画 
     * @param isLong 是否是变长动画
     */
    showScaleAnim(isLong = false) {
        let toY = isLong ? 3 : 1;
        Tween.stopAllByTarget(this._curScale);
        tween(this._curScale).to(0.2, { y: toY }, { easing: 'sineInOut' }).call(() => {
            this._isScaleAnim = false;
        }).start();
    }

    //---------Events----------
    //玩家操作事件
    onPlayerCtrl(d?) {
        if (this._isSpeedUp != d.isSpeedUpBtn) {
            //切换颜色
            let cfg = d.isSpeedUpBtn ? this.colorCfg.speedUp : this.colorCfg.normal;
            this._curColor.set(cfg.c);
            this._floatMaxG = cfg.floatMaxG;
            //缩放动画
            this._isScaleAnim = true;
            this.showScaleAnim(d.isSpeedUpBtn);
        }
        this._isSpeedUp = d.isSpeedUpBtn;
    }

}
